/*********************
* Author:Dzlua
* QQ:505544956
* time:2016/11/06
*/
#include "edui.h"
using namespace edui;

class HandleEvent : public EventHandler
{
public:
    HandleEvent(){}
    ~HandleEvent(){}
public:
    void onButtonCloseClick(Button* btn){
        IWindow* wnd = btn->GetWindow();
        wnd->Close();
    }

    void onButtonMinClick(Button* btn){
        IWindow* wnd = btn->GetWindow();
        wnd->MinWindow();
    }

    void onEditUsrEnter(Edit* edit){
        IWindow* wnd = edit->GetWindow();
        edit->KillFocus();
        /*Edit* pwd = Edit::ToEdit(wnd->FindControl("pwd"));
        if (pwd){
            pwd->SelAllText();
            pwd->SetFocus();
        }*/
    }

    void onEditPwdEnter(Edit* edit){
        IWindow* wnd = edit->GetWindow();
        edit->KillFocus();
        /*Button* button = Button::ToButton(wnd->FindControl("login"));
        if (button) this->onButtonLoginClick(button);*/
    }

    void onEditPwdTextChange(Edit* edit){
        Log("pwd:%s", edit->GetPassWordText());
    }

    void onLinkClick(Link* link){
        Log("Link %s click", link->GetName());
    }

    void onButtonLoginClick(Button* btn){
        IWindow* wnd = btn->GetWindow();
        const char* usr = 0;
        const char* pwd = 0;
        bool bRemberPwd = false;
        bool bAutoLogin = false;
        Edit* edit = Edit::ToEdit(wnd->FindControl("usr"));
        if (edit) if (edit->Verify()) usr = edit->GetText();
        edit = Edit::ToEdit(wnd->FindControl("pwd"));
        if (edit) if (edit->Verify()) pwd = edit->GetPassWordText();
        CheckBox* checkbox = CheckBox::ToCheckBox(wnd->FindControl("remberpwd"));
        if (checkbox) bRemberPwd = checkbox->IsSelected();
        checkbox = CheckBox::ToCheckBox(wnd->FindControl("autologin"));
        if (checkbox) bAutoLogin = checkbox->IsSelected();

        if (usr && pwd){
            Log("Login : usr:%s,pwd:%s,remberPwd:%d,autoLogin:%d", usr, pwd, bRemberPwd, bAutoLogin);
        } else {
            Log("Login : info error!");
        }
    }
};

int main(int argc, char *argv[])
{
    IEnginePtr->Init();

    IEnginePtr->SetStringGroup("English");
    IEnginePtr->SetXmlDir("test/test2/xml/");
    
    if (!IEnginePtr->LoadRes("res/res.xml")){
        Log("[main] : load res error.");
    } else {
        IWindow* wnd = IEnginePtr->CreateWindow("main", "main", WINDOWPOS_CENTERED, WINDOWPOS_CENTERED, 640, 480, WINDOW_BORDERLESS);
        if (wnd){
            if (!wnd->LoadLayout("wnd/main.xml")){
                Log("[main] : load window layout error.");
            } else {
                ISurface* icon = IEnginePtr->FindSurface("logo1");
                wnd->SetIcon(icon);

                HandleEvent evt;
                Button* button = Button::ToButton(wnd->FindControl("min"));
                if (button) button->evt.click.connect(&evt, &HandleEvent::onButtonMinClick);

                button = Button::ToButton(wnd->FindControl("close"));
                if (button) button->evt.click.connect(&evt, &HandleEvent::onButtonCloseClick);

                button = Button::ToButton(wnd->FindControl("login"));
                if (button) button->evt.click.connect(&evt, &HandleEvent::onButtonLoginClick);

                Edit* edit = Edit::ToEdit(wnd->FindControl("pwd"));
                if (edit){
                    edit->evt.onEnter.connect(&evt, &HandleEvent::onEditPwdEnter);
                    edit->evt.textChange.connect(&evt, &HandleEvent::onEditPwdTextChange);
                }

                edit = Edit::ToEdit(wnd->FindControl("usr"));
                if (edit) edit->evt.onEnter.connect(&evt, &HandleEvent::onEditUsrEnter);

                Link* link = Link::ToLink(wnd->FindControl("regist"));
                if (link) link->evt.click.connect(&evt, &HandleEvent::onLinkClick);

                link = Link::ToLink(wnd->FindControl("findpwd"));
                if (link) link->evt.click.connect(&evt, &HandleEvent::onLinkClick);


                wnd->ShowWindow();
                // if not show, SetTextInputRect will error.
                edit->SelAllText();
                edit->SetFocus();
            }
        } else {
            Log("[main] : create window error.");
        }
    }

    IEnginePtr->RunMsgLoop(-1);
    IEnginePtr->Quit();

    return 0;
}
